package DeviceType

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/DeviceType"
	DeviceTypeReq "github.com/flipped-aurora/gin-vue-admin/server/model/DeviceType/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/service"
	"github.com/flipped-aurora/gin-vue-admin/server/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type DeviceTypeApi struct {
}

var deviceTypeService = service.ServiceGroupApp.DeviceTypeServiceGroup.DeviceTypeService

// CreateDeviceType 创建DeviceType
// @Tags DeviceType
// @Summary 创建DeviceType
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body DeviceType.DeviceType true "创建DeviceType"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /deviceType/createDeviceType [post]
func (deviceTypeApi *DeviceTypeApi) CreateDeviceType(c *gin.Context) {
	var deviceType DeviceType.DeviceType
	err := c.ShouldBindJSON(&deviceType)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	verify := utils.Rules{
		"TypeName":        {utils.NotEmpty()},
		"TypeDescription": {utils.NotEmpty()},
	}
	if err := utils.Verify(deviceType, verify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := deviceTypeService.CreateDeviceType(&deviceType); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeleteDeviceType 删除DeviceType
// @Tags DeviceType
// @Summary 删除DeviceType
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body DeviceType.DeviceType true "删除DeviceType"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /deviceType/deleteDeviceType [delete]
func (deviceTypeApi *DeviceTypeApi) DeleteDeviceType(c *gin.Context) {
	var deviceType DeviceType.DeviceType
	err := c.ShouldBindJSON(&deviceType)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := deviceTypeService.DeleteDeviceType(deviceType); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteDeviceTypeByIds 批量删除DeviceType
// @Tags DeviceType
// @Summary 批量删除DeviceType
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除DeviceType"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /deviceType/deleteDeviceTypeByIds [delete]
func (deviceTypeApi *DeviceTypeApi) DeleteDeviceTypeByIds(c *gin.Context) {
	var IDS request.IdsReq
	err := c.ShouldBindJSON(&IDS)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := deviceTypeService.DeleteDeviceTypeByIds(IDS); err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateDeviceType 更新DeviceType
// @Tags DeviceType
// @Summary 更新DeviceType
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body DeviceType.DeviceType true "更新DeviceType"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /deviceType/updateDeviceType [put]
func (deviceTypeApi *DeviceTypeApi) UpdateDeviceType(c *gin.Context) {
	var deviceType DeviceType.DeviceType
	err := c.ShouldBindJSON(&deviceType)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	verify := utils.Rules{
		"TypeName":        {utils.NotEmpty()},
		"TypeDescription": {utils.NotEmpty()},
	}
	if err := utils.Verify(deviceType, verify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := deviceTypeService.UpdateDeviceType(deviceType); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindDeviceType 用id查询DeviceType
// @Tags DeviceType
// @Summary 用id查询DeviceType
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query DeviceType.DeviceType true "用id查询DeviceType"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /deviceType/findDeviceType [get]
func (deviceTypeApi *DeviceTypeApi) FindDeviceType(c *gin.Context) {
	var deviceType DeviceType.DeviceType
	err := c.ShouldBindQuery(&deviceType)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if redeviceType, err := deviceTypeService.GetDeviceType(deviceType.ID); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"redeviceType": redeviceType}, c)
	}
}

// GetDeviceTypeList 分页获取DeviceType列表
// @Tags DeviceType
// @Summary 分页获取DeviceType列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query DeviceTypeReq.DeviceTypeSearch true "分页获取DeviceType列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /deviceType/getDeviceTypeList [get]
func (deviceTypeApi *DeviceTypeApi) GetDeviceTypeList(c *gin.Context) {
	var pageInfo DeviceTypeReq.DeviceTypeSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := deviceTypeService.GetDeviceTypeInfoList(pageInfo); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}
